package com.icedevcloud.upms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icedevcloud.common.core.exception.ApiException;
import com.icedevcloud.common.core.page.PageResult;
import com.icedevcloud.upms.api.dict.dto.DictSimpleResDto;
import com.icedevcloud.upms.controller.admin.dict.dto.AddDictReqDto;
import com.icedevcloud.upms.controller.admin.dict.dto.DictPageReqDto;
import com.icedevcloud.upms.controller.admin.dict.dto.UpdateDictReqDto;
import com.icedevcloud.upms.entity.SysDict;
import com.icedevcloud.upms.mapper.SysDictMapper;
import com.icedevcloud.upms.service.ISysDictService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author XiaoBingBy
 * @since 2023-08-13
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Override
    public PageResult<SysDict> dictPage(DictPageReqDto reqDto) {
        Page<SysDict> page = new Page<>(reqDto.getPage(), reqDto.getPageSize());
        Page<SysDict> pageResult = this.page(
                page,
                Wrappers.<SysDict>lambdaQuery()
                        .eq(reqDto.getPid() != null, SysDict::getPid, reqDto.getPid())
                        .eq(StrUtil.isNotBlank(reqDto.getDictName()), SysDict::getDictName, reqDto.getDictName())
                        .eq(StrUtil.isNotBlank(reqDto.getDictType()), SysDict::getDictType, reqDto.getDictType())
                        .eq(StrUtil.isNotBlank(reqDto.getDictCode()), SysDict::getDictCode, reqDto.getDictCode())
                        .eq(reqDto.getStatus() != null, SysDict::getStatus, reqDto.getStatus())
                        .orderByAsc(SysDict::getSort)
        );

        PageResult<SysDict> result = new PageResult<>();
        result.setRecords(pageResult.getRecords());
        result.setTotal(pageResult.getTotal());
        result.setPage(pageResult.getCurrent());
        result.setPageSize(pageResult.getSize());
        result.setPages(pageResult.getPages());
        return result;
    }

    @Override
    public Boolean add(AddDictReqDto reqDto) {
        if (reqDto.getPid() == 0) {
            SysDict sysDict = this.getOne(
                    Wrappers.<SysDict>lambdaQuery()
                            .eq(SysDict::getPid, reqDto.getPid())
                            .eq(SysDict::getDictType, reqDto.getDictType())
            );
            if (sysDict != null) {
                throw new ApiException("字典类型已存在");
            }
        } else {
            SysDict sysDict = this.getOne(
                    Wrappers.<SysDict>lambdaQuery()
                            .eq(SysDict::getPid, reqDto.getPid())
                            .eq(SysDict::getDictType, reqDto.getDictType())
                            .eq(SysDict::getDictCode, reqDto.getDictCode())
            );
            if (sysDict != null) {
                throw new ApiException("字典编码已存在");
            }
        }
        SysDict saveData = BeanUtil.copyProperties(reqDto, SysDict.class);
        return this.save(saveData);
    }

    @Override
    public Boolean update(UpdateDictReqDto reqDto) {
        SysDict sysDict = this.getById(reqDto.getId());
        if (sysDict == null) {
            throw new ApiException("字典不存在");
        }
        SysDict updateData = BeanUtil.copyProperties(reqDto, SysDict.class);
        return this.updateById(updateData);
    }

    @Override
    public Boolean delete(Long id) {
        SysDict sysDict = this.getById(id);
        if (sysDict == null) {
            throw new ApiException("字典不存在");
        }
        if (sysDict.getPid() == 0) {
            Long count = this.count(
                    Wrappers.<SysDict>lambdaQuery()
                            .eq(SysDict::getPid, id)
            );
            if (count > 0) {
                throw new ApiException("存在子节点删除失败");
            }
        }
        return this.removeById(id);
    }

    @Override
    public List<DictSimpleResDto> getDictByDictType(String dictType) {
        List<SysDict> dictList = this.list(
                Wrappers.<SysDict>lambdaQuery()
                        .eq(SysDict::getDictType, dictType)
                        .ne(SysDict::getPid, 0)
                        .eq(SysDict::getStatus, 1)
                        .orderByAsc(SysDict::getSort)
        );
        return BeanUtil.copyToList(dictList, DictSimpleResDto.class);
    }

    @Override
    public DictSimpleResDto getDictByDictTypeAndDictCode(String dictType, String dictCode) {
        SysDict dict = this.getOne(
                Wrappers.<SysDict>lambdaQuery()
                        .eq(SysDict::getDictType, dictType)
                        .ne(SysDict::getPid, 0)
                        .eq(SysDict::getDictCode, dictCode)
                        .eq(SysDict::getStatus, 1)
        );
        if (dict == null) {
            return null;
        }
        return BeanUtil.copyProperties(dict, DictSimpleResDto.class);
    }

}
